package com.example.bbs.dao.impl;

import com.example.bbs.dao.BaseDao;
import com.example.bbs.dao.CallBack;
import com.example.bbs.dao.LoginDao;
import com.example.bbs.model.User;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @auther: zhangxin
 * @Description:
 */
public class LoginDaoImpl extends BaseDao implements LoginDao {


    @Override
    public User getUserByNameAndPwd(String userName, String userPwd) {

        String sql = "select * from user where user_name = ? and user_pwd = ?";
        List<Object> objects = executeQuery(callBack,sql,userName,userPwd);
        if(objects == null || objects.size() == 0) {
            return null;
        }
        return (User)objects.get(0);
    }

    private CallBack callBack = (rs) -> {
        List<Object> olist = new ArrayList<>();
        try {
            while (rs.next()) {
                olist.add(new User(rs.getInt("user_id"), rs.getString("user_name"),
                        rs.getString("user_pwd"),rs.getTimestamp("create_time")));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return olist;
    };
}
